package com.k.base.jdk.stream;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CollectionUtil {
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static <T> List<T> getDistinctList( Collection<T> c) {
        return c.stream().distinct().collect(Collectors.toList());
    }

    public static <T, K> List<T> getDistinctList(Collection<T> c, Function<? super T, ? extends K> f) {
        return c.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(f, Function.identity(), (p1, p2) -> p1),
                        map -> new ArrayList<>(map.values())
                ));
    }
}
